package org.gaaidou.ptarmigan.springboot.consistency;

//@Component
public class ConsistencyHelper {
//    private final LinkedBlockingQueue<Consistency> consistencyQueue = new LinkedBlockingQueue<>(1000);
//    private final ConcurrentHashMap<String, ConsistencyState> consistencyStateConcurrentHashMap = new ConcurrentHashMap<>(1000);
//
//    @PostConstruct
//    public void init() {
//        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
//            try {
//                Consistency consistency = consistencyQueue.poll(200, TimeUnit.MILLISECONDS);
//                if (null != consistency) {
//                    ConsistencyState consistencyState = consistency.getSyncFn().apply(consistency.getTxId());
//                    if (consistencyStateConcurrentHashMap.contains(consistency.getTxId())) {
//                        consistency.getCompleteFn().accept(consistency.getTxId(), consistencyState);
//                        consistencyStateConcurrentHashMap.remove(consistency.getTxId());
//                    }
//                    if (ConsistencyState.DOING.equals(consistencyState)) {
//                        consistencyQueue.offer(consistency);
//                    } else {
//                        consistency.getCompleteFn().accept(consistency.getTxId(), consistencyState);
//                    }
//                }
//
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }, 0, 200, TimeUnit.MILLISECONDS);
//    }
//
//    public Consistency newConsistency(String txId) {
//        return new Consistency(txId, this);
//    }
//
//    void submit(Consistency consistencyTask) {
//        consistencyQueue.offer(consistencyTask);
//    }
//
//    public void notify(String txId, ConsistencyState consistencyState) {
//        consistencyStateConcurrentHashMap.putIfAbsent(txId, consistencyState);
//    }
}
